/*
 * Database Demo Assistant
 */
function DbdemoAssistant() {
	this.db = null;
	this.dbName = "maddb1";
	this.tableName = "Businesses";
	this.dbVersion = "0.1";
	this.dbDisplay = "Demo";
	this.dbSize = 200000;
	this.dataList = [];
}

/*
 * Scene Assistant: Setup
 *
 * + Call-backs: insertButton
 */
DbdemoAssistant.prototype.setup = function() {
	/* List Widget */
	listItems = [];
	attributes = {
		itemTemplate: "Dbdemo/itemTemplate",
		listTemplate: "Dbdemo/listTemplate",
		swipeToDelete: false,
		renderLimit: 40,
		reorderable: false
	};
	model = {
		items: listItems
	};
	this.controller.setupWidget("listWidget", attributes, model);
	
	/* Button */
	buttonAttributes = {};
	buttonModel = {
		"label": "Insert",
		"buttonClass": "",
		"disabled": false
	};
	
	this.controller.setupWidget("demoButton", buttonAttributes, buttonModel);
	Mojo.Event.listen(this.controller.get("demoButton"), Mojo.Event.tap, this.insertButton.bind(this));
	
	/* Button */
	buttonAttributes = {};
	buttonModel = {
		"label": "Delete",
		"buttonClass": "",
		"disabled": false
	};
	
	this.controller.setupWidget("deleteButton", buttonAttributes, buttonModel);
	Mojo.Event.listen(this.controller.get("deleteButton"), Mojo.Event.tap, this.deleteButton.bind(this));
	
	/* Button */
	buttonAttributes = {};
	buttonModel = {
		"label": "Drop Tables",
		"buttonClass": "",
		"disabled": false
	};
	
	this.controller.setupWidget("dropButton", buttonAttributes, buttonModel);
	Mojo.Event.listen(this.controller.get("dropButton"), Mojo.Event.tap, this.dropTables.bind(this));
}

/*
 * Scene Assistant: Activate
 */
DbdemoAssistant.prototype.activate = function(event) {
	this.loadTheDB();
	this.selectData();
}

/*
 * Scene Assistant: Deactivate
 */
DbdemoAssistant.prototype.deactivate = function(event) {

}

/*
 * Scene Assistant: Clean Up
 */
DbdemoAssistant.prototype.cleanup = function(event) {

}

/*
 * DB Demo: Handle Insert Button Click
 */
DbdemoAssistant.prototype.insertButton = function() {
	this.loadTheDB();
	this.insertData();
	this.selectData();
}

/*
 * DB Demo: Handle Delete Button Click
 */
DbdemoAssistant.prototype.deleteButton = function() {
	this.loadTheDB();
	this.deleteData();
	this.selectData();
}

/*
 * DB Demo: Load The Database
 *
 * + Call-backs: dbLoaded and dbLoadError
 */
DbdemoAssistant.prototype.loadTheDB = function() {
	var values = [];
	try {
		this.db = openDatabase(this.dbName, this.dbVersion, this.dbDisplay, this.dbSize);
	} catch (err) {
		return;
	}
	if (this.db) {
		this.db.transaction(function(transaction) {
			var sql = "CREATE TABLE IF NOT EXISTS 'Businesses' (businessId INTEGER PRIMARY KEY AUTOINCREMENT, businessName TEXT, location TEXT);";
			transaction.executeSql(sql, [], nullDataHandler, this.dbLoadError.bind(this));
			
			var sql = "CREATE TABLE IF NOT EXISTS 'Schedule' (scheduleId INTEGER PRIMARY KEY AUTOINCREMENT, businessId INTEGER, openTime TEXT, closeTime TEXT);";
			transaction.executeSql(sql, [], this.dbLoaded.bind(this), this.dbLoadError.bind(this));
		}
.bind(this));
	}
}

/*
 * DB Demo: Insert Generated Data
 *
 * + Call-backs: dbInsert and dbInsertError
 */
DbdemoAssistant.prototype.insertData = function() {
	if (this.db) {
		this.db.transaction(function(transaction) {
			var sql = "INSERT INTO Businesses (businessName, location) VALUES (?, ?); COMMIT;";
			var values = ['Paradise Salon', 'Boonville, IN'];
			transaction.executeSql(sql, values, this.dbInsert.bind(this), this.dbInsertError.bind(this));
			
			var sql = "INSERT INTO Businesses (businessName, location) VALUES (?, ?); COMMIT;";
			var values = ['Guitar Center', 'Evansville, IN'];
			transaction.executeSql(sql, values, this.dbInsert.bind(this), this.dbInsertError.bind(this));
		}
.bind(this));
	}
}

/*
 * DB Demo: Delete data from Database
 *
 * + Call-backs: dbDeleted and deleteError
 */
DbdemoAssistant.prototype.deleteData = function() {
	var values = [];
	if (this.db) {
		this.db.transaction(function(transaction) {
			var sql = "DELETE FROM Businesses";
			transaction.executeSql(sql, values, this.dbDeleted.bind(this), this.deleteError.bind(this));
		}
.bind(this));
	}
}

/*
 * DB Demo: Drop Tables from Database
 *
 * + Call-backs:
 */
DbdemoAssistant.prototype.dropTables = function() {
	var values = [];
	if (this.db) {
		this.db.transaction(function(transaction) {
			var sql = "DROP TABLE IF EXISTS Schedule;";
			transaction.executeSql(sql, values, nullDataHandler, this.dropError.bind(this));
			
			var sql = "DROP TABLE IF EXISTS Businesses;";
			transaction.executeSql(sql, values, this.tableDropped.bind(this), this.dropError.bind(this));
		}
.bind(this));
	}
}

/*
 * DB Demo: Pull data from Database
 *
 * + Call-backs: displayData and selectError
 */
DbdemoAssistant.prototype.selectData = function() {
	var values = [];
	if (this.db) {
		this.db.transaction(function(transaction) {
			var sql = "SELECT * FROM Entries ORDER BY description";
			transaction.executeSql(sql, values, this.displayData.bind(this), this.selectError.bind(this));
		}
.bind(this));
	}
}

/*
 * DB Demo: Call-back to selectData that displays data in list widget
 */
DbdemoAssistant.prototype.displayData = function(transaction, results) {
	var listItems = [];
	
	var d = new Date();
	
	if (results.rows.length > 0) {
		for (i = 0; i < results.rows.length; i++) {
			//var timeNow = new Date(1984, 0, d.getDay() + 1, d.getHours(), d.getMinutes(), 0);
			var timeNow = new Date(1984, 0, 3, 23, 11, 0);
			var timeNowSeconds = timeNow.getTime();
			var nowDayOfWeek = timeNow.getDate();
			
			var timeOpen = new Date(results.rows.item(i)["timeOpenDay" + nowDayOfWeek]);
			var timeOpenSeconds = timeOpen.getTime();
			var timeClose = new Date(results.rows.item(i)["timeCloseDay" + nowDayOfWeek]);
			var timeCloseSeconds = timeClose.getTime();
			var isOpenThisDay = results.rows.item(i)["isOpenDay" + nowDayOfWeek];
			
			var entryId = results.rows.item(i).entryId;
			var description = results.rows.item(i).description;
			var location = results.rows.item(i).location;
			
			
			Mojo.Log.info(" ");
			Mojo.Log.info(" ");
			Mojo.Log.info("==================================================");
			Mojo.Log.info("Description: " + description);
			Mojo.Log.info("--------------------------------------------------");
			Mojo.Log.info("nowDayOfWeek: " + nowDayOfWeek);
			Mojo.Log.info("--------------------------------------------------");
			Mojo.Log.info("isOpenThisDay: " + isOpenThisDay);
			Mojo.Log.info("--------------------------------------------------");
			Mojo.Log.info("timeNow: " + timeNow);
			Mojo.Log.info("timeNowSeconds: " + timeNowSeconds);
			Mojo.Log.info("--------------------------------------------------");
			Mojo.Log.info("timeOpenDay" + nowDayOfWeek + ":  " + timeOpen);
			Mojo.Log.info("timeOpenDay" + nowDayOfWeek + " Seconds:  " + timeOpenSeconds);
			Mojo.Log.info("--------------------------------------------------");
			Mojo.Log.info("timeCloseDay" + nowDayOfWeek + ": " + timeClose);
			Mojo.Log.info("timeCloseDay" + nowDayOfWeek + " Seconds: " + timeCloseSeconds);
			Mojo.Log.info("--------------------------------------------------");
			
			if (isOpenThisDay == 0) {
				Mojo.Log.info("We are closed all day.");
			} else if ((timeOpenSeconds <= timeNowSeconds) && (timeNowSeconds <= timeCloseSeconds)) {
				Mojo.Log.info("We are currently open.");
			} else {
				Mojo.Log.info("We are closed, at this hour. Checking previous day...");
				
				nowDayOfWeek--;
				
				if (nowDayOfWeek == 0) {
					nowDayOfWeek = 7
					timeNow.setDate(8);
					timeNowSeconds = timeNow.getTime();
				}
				
				timeOpen = new Date(results.rows.item(i)["timeOpenDay" + nowDayOfWeek]);
				timeOpenSeconds = timeOpen.getTime();
				timeClose = new Date(results.rows.item(i)["timeCloseDay" + nowDayOfWeek]);
				timeCloseSeconds = timeClose.getTime();
				
				Mojo.Log.info("--------------------------------------------------");
				Mojo.Log.info("nowDayOfWeek: " + nowDayOfWeek);
				Mojo.Log.info("--------------------------------------------------");
				Mojo.Log.info("timeNow: " + timeNow);
				Mojo.Log.info("timeNowSeconds: " + timeNowSeconds);
				Mojo.Log.info("--------------------------------------------------");
				Mojo.Log.info("timeOpenDay" + nowDayOfWeek + ":  " + timeOpen);
				Mojo.Log.info("timeOpenDay" + nowDayOfWeek + " Seconds:  " + timeOpenSeconds);
				Mojo.Log.info("--------------------------------------------------");
				Mojo.Log.info("timeCloseDay" + nowDayOfWeek + ": " + timeClose);
				Mojo.Log.info("timeCloseDay" + nowDayOfWeek + " Seconds: " + timeCloseSeconds);
				Mojo.Log.info("--------------------------------------------------");
				
				if ((timeOpenSeconds <= timeNowSeconds) && (timeNowSeconds <= timeCloseSeconds)) {
					Mojo.Log.info("We are still open from the previous day.");
				} else {
					Mojo.Log.info("We are definitely closed at this time.");
				}
			}
			
			
			
			
			var newOne = {
				entryId: entryId,
				description: description,
				location: location
			};
			listItems[i] = newOne;
		}
	}
	newModel = {
		items: listItems
	};
	this.controller.setWidgetModel("listWidget", newModel);
}

/*
 * DB Demo: Unused Functions Bound to Call-backs
 */
// Loading the database
DbdemoAssistant.prototype.dbLoaded = function(transaction, results) {
}
DbdemoAssistant.prototype.dbLoadError = function(transaction, error) {
}
// Insert data into the database
DbdemoAssistant.prototype.dbInsert = function(transaction, results) {
}
DbdemoAssistant.prototype.dbInsertError = function(transaction, error) {
}
// Select data from the database
DbdemoAssistant.prototype.selectError = function(transaction, error) {
}
// Delete data from the database
DbdemoAssistant.prototype.dbDeleted = function(transaction, error) {
}
DbdemoAssistant.prototype.deleteError = function(transaction, error) {
}
// Drop tables from the database
DbdemoAssistant.prototype.tableDropped = function(transaction, error) {
}
DbdemoAssistant.prototype.dropError = function(transaction, error) {
}
function nullDataHandler(transaction, results) {
}
